package com.ruoyi.market.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.market.domain.MarketUser;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.market.service.IMarketUserService;
import com.ruoyi.market.util.MarketCallResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 营销用户controller
 * @author pengguochao
 * @Date 2021-12-13
 */
@Slf4j
@RestController
@RequestMapping("/marketUser")
public class MarketUserController {

    @Autowired
    private IMarketUserService marketUserService;

    /**
     * 营销用户电销结果接收
     */
    @PostMapping("/callResult")
    public AjaxResult receiveCallResult(@RequestBody JSONObject jsonObject){
        if(jsonObject.containsKey("data")){
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            List<MarketUser> marketUserList = new ArrayList<>();
            for(int i=0; i<jsonArray.size(); i++){
                JSONObject json = jsonArray.getJSONObject(i);
                MarketUser marketUser = new MarketUser();
                marketUser.setPhoneNumber(json.getString("phoneNumber"));
                marketUser.setCallResult(json.getString("callResult"));
                marketUser.setRemark(json.getString("remark"));
                //需要从calllist 里面 剔除的用户
                if(MarketCallResultUtil.callResultMap.containsKey(json.getString("callResult"))){
                    marketUser.setState("3");
                }
                marketUserList.add(marketUser);
                if(marketUserList.size()==1000 || i==jsonArray.size()-1){
                    marketUserService.updateMarketUser(marketUserList);
                    marketUserList =  new ArrayList();
                }
            }
        }else{
            return AjaxResult.error("请求参数格式错误");
        }
        return AjaxResult.success();
    }
}
